package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.Date;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.common.util.Global;
import com.turing.post.common.util.IdType;
import com.turing.post.common.util.IdTypeDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomBusiness;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.salesmanage.util.RiskLevelChangeDetail;
import com.turing.post.salesmanage.util.RiskLevelChangeDetailDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.IDCardCheck;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "salesmanage.impl.PersonalCustomBusinessImpl")
@Remote
@Local
public class PersonalCustomBusinessImpl implements PersonalCustomBusiness {

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao personalCustomDaoImpl;

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "salesmanage.impl.RiskLevelChangeDetailDaoImpl")
	private RiskLevelChangeDetailDao riskLevelChangeDetailDaoImpl;

	@EJB(mappedName = "common.impl.IdTypeDaoImpl")
	private IdTypeDao idTypeDaoImpl;

	@Override
	public BigInteger insertPersonalCustomInfo(PCPSWebInput webInput,
			PersonalCustom personalCustom) throws PCPSApplicationException {
		if (personalCustom == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"个人客户信息输入为NULL");
		}
		// IssueCompanyNo
		if (PCPSUtil.isEmpty(personalCustom.getIssueCompanyNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"IssueCompanyNo");
		}
		// 证件号
		if (PCPSUtil.isEmpty(personalCustom.getIdNo())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "IdNo");
		}

		Global global = commonDaoImpl.getGlobalInfo();
		IdType idType = idTypeDaoImpl.getIdTypeByIdTypeId(personalCustom
				.getIdTypeId());
		if (idType.getIdTypeName().equals(global.getCommonIdTypeName())) {
			// 验证身份证
			IDCardCheck check = new IDCardCheck();
			if (!check.Verify(personalCustom.getIdNo())) {
				throw new SalesManageApplicationException(
						SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
						SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
						"错误的身份证号码：" + personalCustom.getIdNo());
			}
		}
		// 地区码
		if (PCPSUtil.isEmpty(personalCustom.getNationCodeId())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
					"NationCodeId");
		}
		// 姓名
		if (PCPSUtil.isEmpty(personalCustom.getName())) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "Name");
		}
		PersonalCustom obj = personalCustomDaoImpl.getPersonalCustomByUNI(personalCustom.getIssueCompanyNo(), personalCustom.getIdTypeId(), personalCustom.getIdNo());
		if(obj != null){
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "当前发卡机构下已存在该客户!");
		}
		// 新增操作员
		personalCustom.setOperId(webInput.getOperId());
		// 主键
		BigInteger perId = commonDaoImpl.getUniqueIntegerKey();
		personalCustom.setPersonalId(perId);
		// 新增时间
		personalCustom.setCreateTime(commonDaoImpl.getDatabaseSysTime());
		// 插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "创建个人客户信息", "创建个人客户信息，姓名 "
						+ personalCustom.getName());
		// 插入个人客户信息
		try {
			personalCustomDaoImpl.insertPersonalCustom(personalCustom);
		} catch (PCPSApplicationException p) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "新增用户证件信息已经存在!");
		}
		return perId;
	}

	@Override
	public void updatePersonalCustomInfo(PCPSWebInput webInput,
			PersonalCustom personalCustom) throws PCPSApplicationException {

		PersonalCustom _personalCustom = personalCustomDaoImpl
				.getPersonalCustomByPersonalId(personalCustom.getPersonalId());
		// 对象
		if (_personalCustom == null) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.ORGANIZECUSTOM_ARGS_NULL,
					"待修改个人信息数据库中不存在!");
		}
		// 对象赋值
		_personalCustom.setIssueCompanyNo(personalCustom.getIssueCompanyNo());

		Global global = commonDaoImpl.getGlobalInfo();
		IdType idType = idTypeDaoImpl.getIdTypeByIdTypeId(personalCustom
				.getIdTypeId());
		if (idType.getIdTypeName().equals(global.getCommonIdTypeName())) {
			// 验证身份证
			IDCardCheck check = new IDCardCheck();
			if (!check.Verify(personalCustom.getIdNo())) {
				throw new SalesManageApplicationException(
						SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
						SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL,
						"错误的身份证号码：" + personalCustom.getIdNo());
			}
		}
		_personalCustom.setIdTypeId(personalCustom.getIdTypeId());
		_personalCustom.setIdNo(personalCustom.getIdNo());

		// 修改图片
		_personalCustom.setIdImageId(personalCustom.getIdImageId());
		_personalCustom.setProfessionId(personalCustom.getProfessionId());

		Date time = commonDaoImpl.getDatabaseSysTime();

		// 风险等级
		if (!PCPSUtil.isEmpty(personalCustom.getRiskLevel())
				&& !personalCustom.getRiskLevel().equals(
						_personalCustom.getRiskLevel())) {
			RiskLevelChangeDetail rlcd = new RiskLevelChangeDetail();
			rlcd.setChangeId(commonDaoImpl.getUniqueIntegerKey());
			rlcd.setIssueCompanyNo(PCPSUtil.getIssueCompanyNo(webInput));
			rlcd.setEntityType(RiskLevelChangeDetail.entityType_person);
			rlcd.setEntityID(personalCustom.getPersonalId().toString());
			rlcd.setChangeTime(time);
			rlcd.setOperId(webInput.getOperId());
			rlcd.setOrigRiskLevel(_personalCustom.getRiskLevel());
			rlcd.setNewRiskLevel(personalCustom.getRiskLevel());
			riskLevelChangeDetailDaoImpl.insert(rlcd);
		}
		_personalCustom.setRiskLevel(personalCustom.getRiskLevel());

		_personalCustom.setNationCodeId(personalCustom.getNationCodeId());
		_personalCustom.setName(personalCustom.getName());
		_personalCustom.setLinkAddr(personalCustom.getLinkAddr());
		_personalCustom.setLinkTel(personalCustom.getLinkTel());
		_personalCustom.setEmail(personalCustom.getEmail());
		_personalCustom.setPostCode(personalCustom.getPostCode());
		_personalCustom.setRemark(personalCustom.getRemark());
		// 修改操作员
		_personalCustom.setLastModifyOperId(webInput.getOperId());
		// 修改时间
		_personalCustom.setLastModifyTime(time);

		// 插入日志
		sysOperLogDaoImpl.insertSysOperLogInfo(webInput,
				SysOperLog.operType_Add, "修改个人客户信息", "修改个人客户信息，姓名 "
						+ personalCustom.getName());
		// 插入个人客户信息
		try {
			personalCustomDaoImpl.updatePersonalCustom(_personalCustom);
		} catch (PCPSApplicationException p) {
			throw new SalesManageApplicationException(
					SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
					SalesManageRespCode.PERSONALCUSTOM_ARGS_NULL, "所修改的用户证件号已经存在!");
		}
	}
}